//
//  NBHomeListView.m
//  v2ex-shelves
//
//  Created by Mac os on 2018/4/9.
//  Copyright © 2018年 Mac os. All rights reserved.
//

#import "NBHomeListView.h"

@interface NBHomeListView()

@property(nonatomic,strong)NSMutableArray *lableArray;
@property(nonatomic,strong)UIView *maskBackGroundView;
@property(nonatomic,strong)UIView *bearView;
@property(nonatomic,strong)NSMutableArray *buttonArray;

@end
@implementation NBHomeListView
{
    BOOL _isSpread;
    CGFloat  showHeight;
}
- (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray<WTNode *> *)titleArray
{
    self = [super initWithFrame:frame];
    if (self) {
        _lableArray = [titleArray mutableCopy];
        [self addSubview:self.maskBackGroundView];
        _isSpread = NO;
        [self setupView];
    }
    return self;
}

-(void)setupView
{
    self.bearView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,ScreenWidth, 250)];
    self.bearView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.bearView];
    
    CGFloat marginX = 15;
    CGFloat marginY = 10;
    CGFloat height = 28;
    UIButton * markBtn;
    for (int i = 0; i < _lableArray.count; i++) {
        CGFloat width =  [self calculateString:_lableArray[i] Width:12] +20;
        UIButton * tagBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        if (!markBtn) {
            tagBtn.frame = CGRectMake(marginX, marginY, width, height);
        }else{
            if (markBtn.frame.origin.x + markBtn.frame.size.width + marginX + width + marginX > kScreenWidth) {
                tagBtn.frame = CGRectMake(marginX, markBtn.frame.origin.y + markBtn.frame.size.height + marginY, width, height);
            }else{
                tagBtn.frame = CGRectMake(markBtn.frame.origin.x + markBtn.frame.size.width + marginX, markBtn.frame.origin.y, width, height);
            }
        }
        [tagBtn setTitle:_lableArray[i] forState:UIControlStateNormal];
        tagBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [tagBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self makeCornerRadius:12 borderColor:[UIColor blackColor] layer:tagBtn.layer borderWidth:.5];
        markBtn = tagBtn;
        
        [tagBtn addTarget:self action:@selector(clickTo:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.bearView addSubview:markBtn];
    }
    CGRect rect = self.frame;
    rect.size.height = markBtn.frame.origin.y + markBtn.frame.size.height + marginY;
    self.bearView.frame = rect;
    
}

-(void)makeCornerRadius:(CGFloat)radius borderColor:(UIColor *)borderColor layer:(CALayer *)layer borderWidth:(CGFloat)borderWidth
{
    layer.cornerRadius = radius;
    layer.masksToBounds = YES;
    layer.borderColor = borderColor.CGColor;
    layer.borderWidth = borderWidth;
}
-(CGFloat)calculateString:(NSString *)str Width:(NSInteger)font
{
    CGSize size = [str boundingRectWithSize:CGSizeMake(ScreenWidth, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:font]} context:nil].size;
    return size.width;
}


-(void)clickTo:(UIButton *)sender
{
    
}

-(void)showOrHiddenView
{
    _isShowing = !_isShowing;
    
    if (_isShowing){
        
        [self expandWithTableViewHeight:self.height];
        
    }else
    {
        [self takeBackTableView];
    }
}


-(void)expandWithTableViewHeight:(CGFloat )tableViewHeight
{
    CGRect rect = self.frame;
    [self showSpringAnimationWithDuration:0.3 animations:^{
        self.frame= rect;
        self.bearView.frame = CGRectMake(0, 0, ScreenWidth, tableViewHeight);
        self.maskBackGroundView.alpha =1;
        self.alpha =1;
    } completion:^{
        
    }];
}


-(void)takeBackTableView
{
    [self endEditing:YES];
    CGRect rect = self.frame;
    [self showSpringAnimationWithDuration:.3 animations:^{
        self.frame = rect;
        self.bearView.frame = CGRectMake(0, 0, ScreenWidth,0);;
        self.maskBackGroundView.alpha =0;
        self.alpha =0;
    } completion:^{
    }];
    
}

-(UIView *)maskBackGroundView
{
    if (_maskBackGroundView) {
        return _maskBackGroundView;
    }
    self.maskBackGroundView=[[UIView alloc]initWithFrame:CGRectMake(0,0,ScreenWidth, ScreenHeight - self.frame.origin.y)];
    self.maskBackGroundView.backgroundColor=[UIColorFromRGB(0x000000)colorWithAlphaComponent:0.7];
    self.maskBackGroundView.hidden=NO;
    self.maskBackGroundView.userInteractionEnabled=YES;
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(maskBackGroundViewTapClick)];
    [self.maskBackGroundView addGestureRecognizer:tap];
    return self.maskBackGroundView;
}


-(void)maskBackGroundViewTapClick
{
    _isShowing = NO;
    [self takeBackTableView];
}

-(void)hiddenView{
    
    _isShowing = NO;
    [self takeBackTableView];
}


-(void)showSpringAnimationWithDuration:(CGFloat)duration
                            animations:(void (^)())animations
                            completion:(void (^)())completion
{
    
    
    [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:.8 initialSpringVelocity:5 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
        if (animations) {
            animations();
        }
    } completion:^(BOOL finished) {
        if (completion) {
            completion();
        }
    }];
}
@end
